<?php
/*
* Author: Nguyen Hai Nam
* Module: Config_LanguagesModule
* Desc	: Introduce pick.vn and sidle pic
* Date	: 12/12/2011
*/
class Config_LanguagesModule extends Pick_Core_Module{
	
	public $authentication = 1;
	
	function Config_LanguagesModule(){
		
		parent::Pick_Core_Module();
		$this->theme = "admin";		
		# Languages
		$languages = new Pick_Core_Languages();
		$objectLanguages = "";
		$objectLanguages = $languages->getObjects(1, 'status = 1', array('position'=>'ASC'), '');
		if($objectLanguages){
			$objectLanguages = $objectLanguages['data'];	
		}#endif
		$this->template->assign("objectLanguages", $objectLanguages);
        
        
        // Get Path for this module
        $this->template->assign('arrayParentPath', $this->getArrayParentPath());
	}
	public function getContent(){
		
		$act = $this->request->element('act', "list");
		$this->template->assign('act', $act);
		if($act == "position"){
			$languages = new Pick_Core_Languages();
			$oId = $this->request->element('id','');
			$val = $this->request->element('val','');
			$arrData = array('position'=>$val);
			echo $languages->updateData($arrData, $oId);
			die;
		}
		if($act == "add"){
			$this->addAction();
		}else{
			$this->listAction();	
		}
		
	}
	public function listAction(){
		
		$languages 		= new Pick_Core_Languages();
		$actions 		= new Pick_Core_Actions();
		$actions->setInfoAction($languages, $this->request);
		
		
		$items_per_pages = DEFAULT_ADMIN_ROWS_PER_PAGE;
		$infoText = '';
		$infoClass = 'infoText';
		
		# Read parameters from request
		$plus 			= $this->request->element('plus','');
		$searchTerms 	= $this->request->element('searchTerms','');
		$searchStatus 	= $this->request->element('searchStatus','-1');
		$page 			= $this->request->element('page',1);
		$orderBy 		= $this->request->element('orderBy', 'position');
		$orderDir 		= $this->request->element('orderDir', 'ASC');
		
		# Control plus action
		if($plus){
			if($plus == 'cleanTrash'){
				$languagesDelete = $languages->getObjects(1, 'status = 2');
				if($languagesDelete){
					$languagesDelete = $languagesDelete['data'];
				}
				foreach($languagesDelete as $objLang){
					$languages->deleteFieldIntoTable($objLang->getField("code"));	
				}
			}
			$actions->auto($plus);	
			$infoText 	= $actions->getInfoAction();
			$infoClass 	= "errorText";		
		}
		
		# Build search condition
		$searchCond = "1=1";			# For other cases, you can set this to "(1>0)"
		if($searchStatus != '-1') $searchCond .= " AND status = '$searchStatus'";
		if($searchTerms != '') $searchCond .= " AND (name LIKE '%".$searchTerms."%' OR code LIKE '%".$searchTerms."%')";
		
		# Get all categories object and pass to template
		$listObjects = $languages->getObjects($page, $searchCond, array('position' => 'ASC'));
		
		# Generate status combobox for Search form
		$listStatus = optionStatus($searchStatus);
		
		# Pass some variables to template
		$this->template->assign('page', $page);
		$this->template->assign('listStatus', $listStatus);
		$this->template->assign('listObjects', $listObjects);
		$this->template->assign('searchTerms', $searchTerms);
		$this->template->assign('searchStatus', $searchStatus);
		$this->template->assign('orderBy', $orderBy);
		$this->template->assign('orderDir', $orderDir);
		$this->template->assign('infoText', $infoText);
		$this->template->assign('infoClass', $infoClass);

	}
	
	public function addAction(){
		
		$languages 		= new Pick_Core_Languages();
		$objectInfo 	= new Pick_Core_LanguagesInfo();
		$textfilter 	= new Pick_Data_TextFilter();
		
		$infoText = '';
		$infoClass = '';
		$oId = $this->request->element('oId', 0);
		$plus = $this->request->element('plus','');
		
		# Read parameters from request
		$arrayFieldFromMySQL = $languages->getListFields();
		$object = $languages->getObject($oId);
		
		if(is_object($object) && $this->request->post('plus')==""){
			foreach($arrayFieldFromMySQL as $fieldName){
				$fields[$fieldName] = $object->getField($fieldName);
			}#endforeach		
		}else{
			foreach($arrayFieldFromMySQL as $fieldName){
				$fields[$fieldName] = $this->request->post($fieldName,'');
			}#endforeach
		}
		
		# Modify item of array
		$fields['slug'] = $textfilter->urlize($this->request->element('slug', $fields['name']),false,'-');
		
		# Gan gia tri cho doi tuong
		$objectInfo->setArrayField($fields);
		$this->template->assign("objectInfo", $objectInfo);
		
		# Truy du lieu qua template
		$listStatus = optionStatus($objectInfo->getField('status'), DEFAULT_ADMIN_LANGUAGE);
		$this->template->assign("listStatus",$listStatus);
		
		foreach($arrayFieldFromMySQL as $fieldName){
			$this->template->assign($fieldName, $objectInfo->getField($fieldName));
		}#endforeach
		
		# Begin process data
		if($_POST && $plus == 'saveItem') {
			# Validation
			$validate 	= new Pick_Data_Validate();
			$validatemess = $this->validateData($this->request, $validate);
			if($validatemess == '') {
		
				$objectCheck = "";
				if($oId<=0){
					$objectCheck = $languages->checkDataExits($fields);
				}	   
				if(is_object($objectCheck)){
					$infoText = $this->l('item_saved_error');
					$infoClass = "InfoText";			
				}else{
					
					$codeNewLang = $objectInfo->getField('code');
					if($oId){
						$languages->updateFieldIntoTable($codeNewLang, $object->getField('code'));	
					}else{
						$languages->addNewFieldIntoTable($codeNewLang);	
					}
					$ok = $languages->updateData($objectInfo->getArrayField(),$oId);
					$infoText = $validatemess;
					if($ok){		 
						$infoText = $this->l('item_save_ok');
						$infoClass = "InfoText";
						$oId = $oId?$oId:mysql_insert_id();			
					}            			
				}
				
			}else {
				$infoText = $this->l('error').":<br />".$validatemess;
				$infoClass = "errorText";					
			}
		}
		$this->template->assign("oId",$oId);
		$this->template->assign("infoText",$infoText);
		$this->template->assign("infoClass",$infoClass);

	}
	
	function validateData($request, $validate) {
		$infoText = '';
		$infoText .= $validate->validString($this->l('code'),$request->element('code'));
		return $infoText;
	}
}